package com.yang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.model.Pet;
import com.yang.model.User;
import com.yang.service.PetService;
import com.yang.service.UserService;
import com.yang.mapper.UserMapper;
import com.yang.utlis.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 杨凌霄的大眼睛
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-05-30 19:15:50
*/
@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private PetService petService;

    public static final String SALT = "yanglingxiao";

    @Override
    public User login(String account, String password) {
        if (StringUtils.isAnyBlank(account, password)) {
            return null;
        }
        // 查询该账号用户是否存在
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, account));
        if (user == null) { // 用户不存在
            log.error("登录用户账号不存在");
            return null;
        } else { // 判断密码是否匹配
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());
            if (!user.getPassword().equals(encryptPassword)) {
                return null;
            }
        }
        return user;
    }

    @Override
    public Integer register(String account, String password, String verifyPassword) {
        if (StringUtils.isAnyBlank(account, password, verifyPassword)) {
            return -1;
        }
        if (!password.equals(verifyPassword)) {
            return -3;
        }
        // 查询该账号用户是否存在
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, account));
        if (user != null) {
            return -2;
        }
        // 密码加密
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + password).getBytes());
        // 注册新用户
        user = new User(UUIDUtil.getUUID(), account, encryptPassword);
        userMapper.insert(user);
        return 1;
    }

    @Override
    public List<Map<String, Object>> getAll() {
        List<Pet> petList = petService.list(new LambdaQueryWrapper<Pet>().orderByDesc(Pet::getSpeed));
        List<Map<String, Object>> res = petList.stream().map(pet -> {
            Map<String, Object> info = new HashMap<>();
            User user = userMapper.selectById(pet.getOwnerId());
            Integer score = (pet.getAttack() + pet.getSpeed() + pet.getDefence()) / 3;
            info.put("username", user.getAccount());
            info.put("score", score);
            return info;
        }).collect(Collectors.toList());
        return res;
    }
}




